/* t_filesys.cpp : test dgnsc file system
 * Copyright (C) 2009-2010 drangon <drangon.zhou@gmail.com>
 * 2011-02
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <dgnsc/filesys.h>
#include <dgnsc/common.h>

#include "gtest.h"

#include <stdio.h>
#include <assert.h>

TEST( filesys, filesys )
{
	int ret;
	dgn_file_t fd = dgn_file_init( "file_to_test.txt", DGN_O_OPEN_CREATE );
	dgn_file_truncate( fd, 0 );
	dgn_file_seek( fd, 0, DGN_SEEK_SET );
	dgn_file_write( fd, "hello", 5 );
	printf( "before lock\n" );
	ret = dgn_file_lock( fd );
	printf( "after lock, ret %d\n", ret );
	dgn_sleepms( 1000 );
	ret = dgn_file_unlock( fd );
	printf( "after unlock, ret %d\n", ret );
	dgn_file_fini( fd ), fd = NULL;

	dgn_file_t fd2 = dgn_file_init( "file_to_test.txt", DGN_O_OPEN_CREATE_EXCL );
	EXPECT_TRUE( fd2 == NULL );

	dgn_file_t fd3 = dgn_file_init( "file_to_test.txt", DGN_O_OPEN_READ );
	ret = dgn_file_write( fd3, "world", 5 );
	EXPECT_TRUE( ret < 0 );

	char buf[256] = {0};
	ret = dgn_file_read( fd3, buf, 256 );
	printf( "read file ret %d, buf [%s]\n", ret, buf );
	dgn_file_fini( fd3 ), fd3 = NULL;

	dgn_file_rm( "file_to_test.txt" );
	fd2 = dgn_file_init( "file_to_test.txt", DGN_O_OPEN_WRITE );
	EXPECT_TRUE( fd2 == NULL );
	fd2 = dgn_file_init( "file_to_test.txt", DGN_O_OPEN_CREATE_EXCL );
	EXPECT_TRUE( fd2 != NULL );
	dgn_file_fini( fd2 ), fd2 = NULL;
}

